package com.teamall.web.main.interceptor;

import com.teamall.core.common.BaseController;
import com.teamall.web.main.vo.LoginUserInfo;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author
 * @create 2017-07-28 23:20
 **/
public class SessionInterceptor extends HandlerInterceptorAdapter {
    private List<String> excludedUrls;

    public List<String> getExcludedUrls() {
        return excludedUrls;
    }

    public void setExcludedUrls(List<String> excludedUrls) {
        this.excludedUrls = excludedUrls;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        String requestUri = request.getRequestURI();
        for (String url : excludedUrls) {
            if (requestUri.indexOf(url) != -1) {
                return true;
            }
        }
        String webPath = request.getContextPath();
        if (request.getSession().getAttribute("loginUserInfo") == null) {
            response.sendRedirect(webPath + "/main/logout");
            return false;
        }
        if(requestUri.equals("/govpover")||requestUri.equals("/govpover/")){
            response.sendRedirect(webPath + "/main/index");
            return false;
        }
        return true;
    }

}
